import axios from 'axios'
import store from '@/store'
import { Message } from 'element-ui'
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 100000
})
service.interceptors.request.use((config) => {
  if (store.getters.token) {
    config.headers.Authorization = `Bearer ${store.getters.token} `
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

service.interceptors.response.use((response) => {
  if (response.data instanceof Blob) return response.data
  const { Data, Msg, Success } = response.data
  if (Success) {
    return Data
  } else {
    Message({
      type: 'error',
      message: Msg
    })
    return Promise.reject(new Error(Msg))
  }
}, (error) => {
  Message({
    type: 'error',
    massage: error.message
  })
  return Promise.reject(error)
})

export default service
